Behaviors
Behaviors
are used to control the internal workings of a service. They are
classes that extend or modify a service type and define the runtime
aspect of the service. WCF behaviors can be applied to one or more
services and are classified accordingly. Behaviors are considered the
primary part of WCF extensibility.
The WCF platform provides a
suite of built-in behaviors that address common behavioral
customizations. An example is the transaction behavior, which is used
to control and auto-complete transaction activities. Other behaviors
include concurrency, instancing, throttling, thread-binding, faults,
exceptions, metadata customization, instance pooling and JITA,
impersonation, authorization, and security.
With the Service Abstraction principle, service-orientation emphasizes the need to hide what lies
beneath a service, which is why service contracts are positioned to
abstract underlying service logic and implementation details. Within
this context, WCF behaviors are used to model the “behind-the-scenes”
service implementation.
|
Behaviors can be applied to a service and operation using the ServiceBehavior and OperationBehavior attributes. Behaviors can also be used to extend and modify channels and endpoints.
The following
example augments the default service implementation by changing its
instance management and concurrency mode behaviors:
Example 1.
[ServiceContract] [ServiceBehavior( InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Multiple)] public interface IGreetings { [OperationContract] string Greet(); }
|
Note
Custom behaviors can be defined by implementing the IServiceBehavior class.
Instancing
The InstanceContextMode parameter is used to control how instances are created, wheras the ConcurrencyMode parameter indicates the threading model parameter used by the service. In Example 1, the service is a singleton with concurrency set to mutithreaded.
Throttling controls, another
example of a configurable service behavior, are used to place usage
limits on a service to prevent over-consumption. Throttling controls
can place limits on the number of concurrent calls, connections,
instances, and pending operations. This form of throttling is carried
out on a per service type and affects all instances of the service and
all of its endpoints. Unlike the InstanceContextMode parameter, which is configured in code, the throttling behavior is configured in the configuration file:
Example 2.
<behaviors> <behavior name="ThrottlingBehavior"> <throttling maxConcurrentCalls="5" maxConnections="10" maxInstances="10" maxPendingOperations="10" /> </behavior> </behaviors>
|
Instancing is used to
control how many instances of a service can run at the same time. WCF
includes four instance models, as listed in Table 1. Instancing is enabled using service behaviors and the InstanceContextMode parameter.
Table 1. A list of WCF instance models.
Instance Models | Description |
---|
per call | the
default behavior of a service (an instance of the service is created
per client call requiring that the per call option of an operation be
stateless) |
per session | one instance of the service is created per session and cannot be shared across sessions |
shareable | one instance of the service is created per session and can be shared across sessions |
single | only
one instance of the service is created for all incoming service calls
(an option used primarily to implement singleton services) |